package hjj.servlet;

import hjj.service.LoginManager;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("name");
		String password = request.getParameter("pass");
		if (username != null && username != "") {
			LoginManager lm = new LoginManager();
			User user = lm.loginCheck(username, password);
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			session.setAttribute("username", user.getUsername());
			session.setAttribute("role", user.getRole().getId());
			session.setAttribute("power", user.getPower().getId());
		}
		request.getRequestDispatcher("/admin/index.jsp").forward(request,
				response);
	}
}
